package com.example.memoapp.activities;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.example.memoapp.R;
import com.example.memoapp.database.MemoDatabaseHelper;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.memoapp.R;
import com.example.memoapp.database.MemoDatabaseHelper;

public class MemoDetailActivity extends AppCompatActivity {

    private TextView tvTitle, tvContent, tvDate;
    private Button btnEdit;
    private int memoId;
    private MemoDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memo_detail);

        tvTitle = findViewById(R.id.tv_memo_title);
        tvContent = findViewById(R.id.tv_memo_content);
        tvDate = findViewById(R.id.tv_memo_date);
        btnEdit = findViewById(R.id.btn_edit_memo);

        memoId = getIntent().getIntExtra("memoId", -1);
        dbHelper = new MemoDatabaseHelper(this);

        if (memoId != -1) {
            loadMemoDetails();
        } else {
            Toast.makeText(this, "无效的备忘录ID", Toast.LENGTH_SHORT).show();
            finish();
        }

        btnEdit.setOnClickListener(v -> {
            Intent intent = new Intent(MemoDetailActivity.this, EditMemoActivity.class);
            intent.putExtra("memoId", memoId);
            startActivity(intent);
        });
    }

    private void loadMemoDetails() {
        Cursor cursor = dbHelper.getMemoById(memoId);
        if (cursor.moveToFirst()) {
            String title = cursor.getString(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_TITLE));
            String content = cursor.getString(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_CONTENT));
            String date = cursor.getString(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_DATE));

            tvTitle.setText(title);
            tvContent.setText(content);
            tvDate.setText(date);
        }
        cursor.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.memo_detail_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_edit) {
            // 打开编辑备忘录界面
            Intent intent = new Intent(this, EditMemoActivity.class);
            intent.putExtra("memoId", memoId);
            startActivity(intent);
            return true;
        } else if (id == R.id.action_delete) {
            // 删除备忘录
            dbHelper.deleteMemo(memoId);
            Toast.makeText(this, "备忘录已删除", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MemoDetailActivity.this, MainActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}    